package com.demo.config;

import com.demo.filter.TestFilter1;
import com.demo.filter.TestFilter2;
import com.demo.interceptor.TestInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description web.xml配置类
 * @Author Dang Xianyue
 * @Date 2020-08-08 11:50
 * @Version 1.0
 */
@Configuration
public class SpringWebConfig implements WebMvcConfigurer {

    //配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TestInterceptor())
                .addPathPatterns("/test/**")
                .excludePathPatterns("/test/test2");
    }

    //配置过滤器
    @Bean
    public FilterRegistrationBean newFilterRegistrationBean1() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new TestFilter1());
        bean.setName("TestFilter1");
        bean.addUrlPatterns("/*");
        bean.setOrder(2);
        return bean;
    }

    @Bean
    public FilterRegistrationBean newFilterRegistrationBean2() {
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new TestFilter2());
        bean.setName("TestFilter2");
        bean.addUrlPatterns("/test/*");
        bean.setOrder(1);
        return bean;
    }
}
